package com.example.dangerouscargodriver.widget;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.appcompat.app.AlertDialog;

import com.example.dangerouscargodriver.R;

public class ModifyPhoneNoDialog{

    private static final int VERIFY_CODE_COUNT = 0;

    private TextView mPhoneNo = null;
    private TextView mVCode = null;
    private Button mGetVCode = null;
    private Context mContext = null;
    private AlertDialog mAlertDialog = null;
    private AlertDialogBtnClickListener mListener = null;
    private WorkHandler mHandler = null;

    public ModifyPhoneNoDialog(Context context){
        mContext = context;
        mHandler = new WorkHandler(mContext.getMainLooper());
    }

    public void showAlertDialog(boolean cancelableTouchOut) {
        View view = LayoutInflater.from(mContext).inflate(
                R.layout.layout_modify_telephone_alert_dialog, null);

        TextView positiveButton = view.findViewById(R.id.alert_dialog_positive_text);
        TextView negativeButton = view.findViewById(R.id.alert_dialog_negative_text);
        mPhoneNo = view.findViewById(R.id.ed_phone_no);
        mVCode = view.findViewById(R.id.ed_verify_no);
        mGetVCode = view.findViewById(R.id.btn_get_vcode);
        mGetVCode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Message msg = new Message();
                msg.what = VERIFY_CODE_COUNT;
                msg.arg1 = 60;
                mHandler.sendMessage(msg);
            }
        });

        positiveButton.setOnClickListener(v -> {
            if(mListener != null) {
                mListener.clickPositive(mPhoneNo.getText().toString(),
                        mVCode.getText().toString());
            }
            mAlertDialog.dismiss();
        });
        negativeButton.setOnClickListener(v -> {
            mAlertDialog.dismiss();
        });
        AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
        builder.setView(view);

        builder.setCancelable(true);   //返回键dismiss
        //创建对话框
        mAlertDialog = builder.create();
        mAlertDialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);//去掉圆角背景背后的棱角
        mAlertDialog.setCanceledOnTouchOutside(cancelableTouchOut);   //失去焦点dismiss
        mAlertDialog.show();
    }

    public void setClickListener(AlertDialogBtnClickListener listener){
        mListener = listener;
    }

    public interface AlertDialogBtnClickListener {
        void clickPositive(String phoneNo, String vcode);

        void clickNegative();
    }

    private class WorkHandler extends Handler {
        private static final String TAG = "WorkHandler";

        public WorkHandler(Looper looper) {
            super(looper);
        }

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            switch (msg.what){
                case VERIFY_CODE_COUNT:
                    int count = msg.arg1;
                    count -= 1;
                    if(count > 0) {
                        mGetVCode.setText(String.valueOf(count));
                        Message delaymsg = new Message();
                        delaymsg.what = VERIFY_CODE_COUNT;
                        delaymsg.arg1 = count;
                        mHandler.sendMessageDelayed(delaymsg,1000);
                    }
                    else{
                        mGetVCode.setText("重新获取");
                    }
                    break;
            }
        }
    }
}